import React from 'react';
import BaseModule from "../common/BaseModule";
import Service from "./Service";
import Lng from "../common/lng";
import {Popconfirm} from "antd";
import AddDishSpecForm from "./AddDishSpecForm";
import EditDishSpecForm from './EditDishSpecForm';

class DishSpec extends BaseModule {
    constructor(props) {
        super(props);
        this.state.id = "";
    }


    loadData() {
        this._page.items = [];
        const value = this.refs.input.input.value;
        this._page.items.push({
            queryKey: "code",
            queryValue: value,
            conditionType: "LIKE"
        });

        super.superLoadData(Service.listDishSpecs);
    }

    addClick() {
        this.refs["dishSpec"].showModal();
    }

    remove(record) {
        const self = this;
        Service.removeDishSpecs(record.id, () => {
            self.loadData();
        })
    }

    edit(record) {
        this.setState({
            id: record.id
        }, () => {
            this.refs["specEdit"].showModal();
        });
    }

    validate(values) {
        delete values["method"];
        return true;
    }

    render() {
        const columns = [{
            title: Lng.get("dishSpec.code"),
            dataIndex: 'code',
            key: 'code'
        }, {
            title: Lng.get("dishSpec.length"),
            dataIndex: 'length',
            key: 'length',
            render: (text) => {
                return text || '-';
            }
        }, {
            title: Lng.get("dishSpec.width"),
            dataIndex: 'width',
            key: 'width',
            render: (text) => {
                return text || '-';
            }
        }, {
            title: Lng.get("dishSpec.volume"),
            dataIndex: 'volume',
            key: 'volume',
            render: (text) => {
                return text || '-';
            }
        },{
            title: Lng.get("dishSpec.count"),
            dataIndex: 'count',
            key: 'count',
            render: (text) => {
                return text || '-';
            }
        }, {
            title: Lng.get("dishSpec.unit"),
            dataIndex: 'unit',
            key: 'sizeUnit',
            render: (text, record) => {
                if (record["volumeUnit"] != null) {
                    return Lng.get("dishSpec."+record.volumeUnit);
                } else if (record["sizeUnit"] != null) {
                    return Lng.get("dishSpec."+record.sizeUnit);
                } else {
                    return Lng.get("dishSpec.G");
                }
            }
        }, {
            title: Lng.get("common.operation"),
            dataIndex: 'operation',
            key: 'operation',
            render: (text, record, index) => {
                return <div className={"operation"}>
                    <a onClick={this.edit.bind(this, record)}>{Lng.get("common.edit", {name: ""})}</a>
                    <Popconfirm title={Lng.get("common.confirm")} onConfirm={this.remove.bind(this, record)}
                                okText={Lng.get("common.ok")}
                                cancelText={Lng.get("common.cancel")}>
                        <a href={"#"}>{Lng.get("common.remove")}</a>
                    </Popconfirm>
                </div>
            }
        }];
        return (
            <div id={"dish-spec-content"}>
                {this.renderSearchBar(Lng.get("dishSpec.code"))}
                {this.renderTable(columns)}
                {this.renderModal("dishSpec", AddDishSpecForm, Lng.get("dishSpec.main"), Service.addDishSpec, null, this.validate.bind(this))}
                {this.renderEditModal("specEdit", EditDishSpecForm, Lng.get("dishSpec.main"), this.state.id, Service.updateDishSpecs)}
            </div>

        )
    }
}

export default DishSpec;